#include "ConsoleEvents.h"
#include <windows.h>
#include <stdlib.h>
#include <iostream>
#include "screen.h"
using namespace std;

DWORD WINAPI ConsoleEventsHook(LPVOID Prm);

HANDLE ConsoleEventsThreadHandle;
DWORD  ConsoleEventsThreadId;

ConsoleEvents* ConsoleEvents::console_instance = NULL;

HANDLE ConsoleEvents::Initalize()
{

	ConsoleEventsThreadHandle = CreateThread(NULL,0,ConsoleEventsHook,this,CREATE_SUSPENDED,&ConsoleEventsThreadId);
	ResumeThread(ConsoleEventsThreadHandle);
	return ConsoleEventsThreadHandle;
}

void ConsoleEvents::Finalize()
{
	CloseHandle(ConsoleEventsThreadHandle);
}


DWORD WINAPI ConsoleEventsHook(LPVOID Prm)
{
int nx,ny;
ConsoleEvents * CE = (ConsoleEvents*)Prm;
DWORD numEvents = 100;
INPUT_RECORD eventBuffer[100];
DWORD numEventsRead = 0;
bool appIsRunning = true;
HANDLE rHnd;    // Handle to read from the console.
rHnd = GetStdHandle(STD_INPUT_HANDLE);
SetConsoleMode(rHnd,ENABLE_MOUSE_INPUT);
    while (appIsRunning) {
        GetNumberOfConsoleInputEvents(rHnd, &numEvents);
        if (numEvents!=0) {
            //INPUT_RECORD *eventBuffer = new INPUT_RECORD[numEvents];

			ReadConsoleInput(rHnd, eventBuffer, numEvents, &numEventsRead);

            for (int i=0; i<numEventsRead; ++i) {

                if (eventBuffer[i].EventType==KEY_EVENT) 
				{
					//if (eventBuffer[i].Event.KeyEvent.bKeyDown)
					//	iData.KeyPressed = true;
					//else
					//	iData.KeyPressed = false;

				if (eventBuffer[i].Event.KeyEvent.bKeyDown)
				{
					if (eventBuffer[i].Event.KeyEvent.wVirtualKeyCode==VK_ESCAPE)
						CE->bexit = true;
					if (eventBuffer[i].Event.KeyEvent.wVirtualKeyCode==VK_SPACE)
					{
 						CE->first_second = (!CE->first_second);
   						CE->NotifyAll();
					}	

					if (eventBuffer[i].Event.KeyEvent.wVirtualKeyCode==VK_DOWN)
					{
						CE->d1++;
						CE->NotifyAll();
					}
					if (eventBuffer[i].Event.KeyEvent.wVirtualKeyCode==VK_UP)
					{
						CE->d1--;
						CE->NotifyAll();
					}
					if (eventBuffer[i].Event.KeyEvent.wVirtualKeyCode==VK_RIGHT)
					{
						CE->d2++;
						CE->NotifyAll();
					}
					if (eventBuffer[i].Event.KeyEvent.wVirtualKeyCode==VK_LEFT)
					{
						CE->d2--;
						CE->NotifyAll();
					}
				}
				 
	
                } 
				else if (eventBuffer[i].EventType==MOUSE_EVENT) {
                    // Set the index to our buffer of CHAR_INFO
                    int offsetPos =
                        eventBuffer[i].Event.MouseEvent.dwMousePosition.X 
                        + 80 * eventBuffer[i].Event.MouseEvent.dwMousePosition.Y;
					
					nx = eventBuffer[i].Event.MouseEvent.dwMousePosition.X;
					ny = eventBuffer[i].Event.MouseEvent.dwMousePosition.Y;
					if (nx !=CE->mx || CE->my!=ny)
					{
						CE->mx = nx;
						CE->my = ny;
						CE->NotifyAll();
					}
					//G->InvokedEvent(); 
                    // Is it a left click?
                    if (eventBuffer[i].Event.MouseEvent.dwButtonState 
                        & FROM_LEFT_1ST_BUTTON_PRESSED) 
					{
						//G->InvokedEvent(); 
					} 
					else if (eventBuffer[i].Event.MouseEvent.dwButtonState 
                        & RIGHTMOST_BUTTON_PRESSED) 
					{
						// G->InvokedEvent(); 
					}
					else if (eventBuffer[i].Event.MouseEvent.dwButtonState 
                        & FROM_LEFT_2ND_BUTTON_PRESSED) 
					{
                    
					}
                }
            }

            // Clean up our event buffer:
            //delete[] eventBuffer;
			Sleep(100);
        }
    }


return 0;
}
ConsoleEvents *Events()
{
	return ConsoleEvents::GetInstance();
}